package com.gimi.cloud.jimi.client.service.userInfo.impl;

import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dao.dao.User.UserInfoDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.client.service.userInfo.UserInfoService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.dto.userInfo.RdUserInfoDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdUserInfoEntity;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public void addUserInfo(RdUserInfoDTO userInfoDTO) {
		userInfoDTO.setPhoto(AliOSSUtil.getFilePath(userInfoDTO.getPhoto()));
		userInfoDTO.setCardFront(AliOSSUtil.getFilePath(userInfoDTO.getCardFront()));
		userInfoDTO.setCardBack(AliOSSUtil.getFilePath(userInfoDTO.getCardBack()));
		userInfoDTO.setCardHand(AliOSSUtil.getFilePath(userInfoDTO.getCardHand()));
		RdUserInfoEntity record = new RdUserInfoEntity();
		BeanUtils.copyProperties(userInfoDTO, record);
		int i = userInfoDao.insert(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}
	
	@Override
	public RdUserInfoDTO getUserInfoSingle(RdUserInfoDTO userInfoDTO) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByUserId(userInfoDTO.getUserId());
		RdUserInfoDTO dto = new RdUserInfoDTO();
		BeanUtils.copyProperties(userInfoEntity, dto);

		dto.setCardHand(AliOSSUtil.getFileUrl(dto.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setCardBack(AliOSSUtil.getFileUrl(dto.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setCardFront(AliOSSUtil.getFileUrl(dto.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setPhoto(AliOSSUtil.getFileUrl(dto.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setAvatar(AliOSSUtil.getFileUrl(dto.getAvatar(), AliOSSUtil.getNoEffictDateByDay(1)));
		return dto;
	}

	@Override
	public void updateUserInfo(RdUserInfoDTO userInfoDTO) {
		RdUserInfoEntity record = new RdUserInfoEntity();
		BeanUtils.copyProperties(userInfoDTO, record);
		int i = userInfoDao.updateByPrimaryKeySelective(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}

	@Override
	public RdUserInfoDTO getUserInfoByUserId(Integer userId) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByUserId(userId);
		RdUserInfoDTO dto = new RdUserInfoDTO();
		if(userInfoEntity != null) {
			BeanUtils.copyProperties(userInfoEntity, dto);

			dto.setCardHand(AliOSSUtil.getFileUrl(dto.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setCardBack(AliOSSUtil.getFileUrl(dto.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setCardFront(AliOSSUtil.getFileUrl(dto.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setPhoto(AliOSSUtil.getFileUrl(dto.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setAvatar(AliOSSUtil.getFileUrl(dto.getAvatar(), AliOSSUtil.getNoEffictDateByDay(1)));
		}
		return dto;
	}

	@Override
	public RdUserInfoDTO getUserBycardId(String cardId) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByCardId(cardId);
		if(userInfoEntity!=null){
			RdUserInfoDTO dto = new RdUserInfoDTO();
			BeanUtils.copyProperties(userInfoEntity, dto);

			dto.setCardHand(AliOSSUtil.getFileUrl(dto.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setCardBack(AliOSSUtil.getFileUrl(dto.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setCardFront(AliOSSUtil.getFileUrl(dto.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setPhoto(AliOSSUtil.getFileUrl(dto.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
			dto.setAvatar(AliOSSUtil.getFileUrl(dto.getAvatar(), AliOSSUtil.getNoEffictDateByDay(1)));
			return dto;
		}
		return null;
	}

	@Override
	public Map<String, Object> getAuthenticationInfo(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByUserId(userId);
		//无数据
		if (userInfoEntity == null || StringUtil.isBlank(userInfoEntity.getCardId())) {
			map.put("status", 0);
			return map;
		}
		RdUserInfoDTO effctionUserInfo = userInfoDao.isEffection(userId);

		if(effctionUserInfo != null) {
			/**
			 * 设置图片地址
			 */
			//头像
			effctionUserInfo.setPhoto(AliOSSUtil.getFileUrl(effctionUserInfo.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
			//身份证头像
			effctionUserInfo.setAvatar(AliOSSUtil.getFileUrl(effctionUserInfo.getAvatar(), AliOSSUtil.getNoEffictDateByDay(1)));
			//身份证正面
			effctionUserInfo.setCardFront(AliOSSUtil.getFileUrl(effctionUserInfo.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
			//身份证背面
			effctionUserInfo.setCardBack(AliOSSUtil.getFileUrl(effctionUserInfo.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
			//手持身份证
			effctionUserInfo.setCardHand(AliOSSUtil.getFileUrl(effctionUserInfo.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
			//学生证照片
			effctionUserInfo.setStudentCard(AliOSSUtil.getFileUrl(effctionUserInfo.getStudentCard(), AliOSSUtil.getNoEffictDateByDay(1)));
		}
		//有数据未过期
		if (userInfoEntity != null && userInfoEntity.getCardId() != null && effctionUserInfo != null && effctionUserInfo.getCardId()!= null ) {
			map.put("status", 1);
			map.put("data", effctionUserInfo);
			return map;
		}else {
			map.put("status", 2);
			return map;
		}
		
	}
}
